package game;

/**
 * The BasicGrid class provides a simple implementation of the Grid interface.
 */
public class BasicGrid implements Grid {
	
	protected int rowsCount;
	protected int colsCount;
	protected char[][] gridState;
	
	public BasicGrid(int _rowsCount, int _colsCount) {
		rowsCount = _rowsCount;
		colsCount = _colsCount;
		gridState = new char[rowsCount][colsCount];
		for(int i = 0; i < rowsCount; ++i) {
			for(int j = 0; j < colsCount; ++j) {
				gridState[i][j] = BLANK;
			}
		}
	}
	
	public BasicGrid(char[][] state) {
		fromState(state);
	}
	
	public BasicGrid() {}

	
	@Override
	public int getColsNCount() {return colsCount;}

	@Override
	public int getRowsCount() {return rowsCount;}
	
	@Override
	public Grid clone()
	{
		return new BasicGrid(toState());
	}

	@Override
	public Grid write(int row, int col, String word, Direction direction) {
		Grid g = clone();
		char[][] newState = g.getState();
		int deltaRow = (direction == Direction.VERTICAL ? 1 : 0);
		int deltaCol = (direction == Direction.HORIZONTAL ? 1 : 0);
		char c = newState[row][col];
		
		if (c == BLACK) return null;
		
		for (int i = 0;
				i < word.length()
					&& row < rowsCount
					&& col < colsCount;
				++i) {
			newState[row][col] = word.charAt(i);
			row += deltaRow;
			col += deltaCol;
		}
		return g;
	}

	@Override
	public Grid writeBlack(int row, int col) {
		char[][] newState = toState();
		newState[row][col] = BLACK;
		return new BasicGrid(newState);
	}

	@Override
	public String getWord(int row, int col, Direction direction) {
		int deltaRow = (direction == Direction.VERTICAL ? 1 : 0);
		int deltaCol = (direction == Direction.HORIZONTAL ? 1 : 0);
		char c = gridState[row][col];
		String result = "";
		
		if (c == BLACK) return null;
		
		while (row < rowsCount && col < colsCount && (c = gridState[row][col]) != BLACK) {
			result += c;
			row += deltaRow;
			col += deltaCol;
		}
		return result.length() > 1 ? result : null;
	}
	
	@Override
	public char[][] getState() {
		return gridState;
	}

	@Override
	public char[][] toState() {
		char[][] r = new char[rowsCount][colsCount];
		for(int i = 0; i < rowsCount; ++i) {
			for(int j = 0; j < colsCount; ++j) {
				r[i][j] = gridState[i][j];
			}
		}
		return r;
	}

	@Override
	public void fromState(char[][] state) {
		rowsCount = state.length;
		colsCount = state[0].length;
		gridState = state;
	}
	
	@Override
	public String toString() {
		String r = "";
		for(int i = 0; i < rowsCount; ++i) {
			for(int j = 0; j < colsCount; ++j) {
				r += gridState[i][j]==BLANK?"?":gridState[i][j];
			}
			r += '\n';
		}
		return r;
	}

	@Override
	public boolean isFull() {
		return 0 == blankCount();
	}
	
	/**
	 * Count the blank in the grid.
	 * @return the number of blank cell
	 */
	private int blankCount() {
		int c = 0;
		for(int i = 0; i < rowsCount; ++i) {
			for(int j = 0; j < colsCount; ++j) {
				if (gridState[i][j] == BLANK) ++c;
			}
		}
		return c;
	}
	
		
	public static void main( String[] args) {
		Grid g = new BasicGrid(5, 5);
		g = g.write(0, 0, "TUEES", Direction.VERTICAL);
		g = g.write(0, 0, "TUEES", Direction.HORIZONTAL);
		System.out.println(g);
		System.out.println(g.getWord(0, 1, Direction.VERTICAL));
		
	}


	
}
